{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于模型的协同过滤的推荐"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "from sklearn.preprocessing import normalize\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from w_svd_cf import SVD_CF"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 读入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('triplet_dataset_sub_song_merged_data_sub.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>83554</td>\n",
       "      <td>26</td>\n",
       "      <td>0.0012</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>83554</td>\n",
       "      <td>44</td>\n",
       "      <td>0.0001</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>83554</td>\n",
       "      <td>103</td>\n",
       "      <td>0.0001</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>83554</td>\n",
       "      <td>187</td>\n",
       "      <td>0.0001</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>83554</td>\n",
       "      <td>303</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    user  song  listen_count                          title  \\\n",
       "0  83554    26        0.0012               You And Me Jesus   \n",
       "1  83554    44        0.0001  Harder Better Faster Stronger   \n",
       "2  83554   103        0.0001                       Uprising   \n",
       "3  83554   187        0.0001         Breakfast At Tiffany's   \n",
       "4  83554   303        0.0007          Lucky (Album Version)   \n",
       "\n",
       "                                 release                  artist_name  year  \n",
       "0                   Tribute To Jake Hess                    Jake Hess  2004  \n",
       "1                              Discovery                    Daft Punk  2007  \n",
       "2                               Uprising                         Muse     0  \n",
       "3                                   Home          Deep Blue Something  1993  \n",
       "4  We Sing.  We Dance.  We Steal Things.  Jason Mraz & Colbie Caillat     0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5535035 entries, 0 to 5535034\n",
      "Data columns (total 7 columns):\n",
      "user            int64\n",
      "song            int64\n",
      "listen_count    float64\n",
      "title           object\n",
      "release         object\n",
      "artist_name     object\n",
      "year            int64\n",
      "dtypes: float64(1), int64(3), object(3)\n",
      "memory usage: 295.6+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3321021, 7)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "# 随机采样20%的数据构建测试集，其余60%作为训练集\n",
    "data_train, data_test  = train_test_split(data, random_state=33, test_size=0.4)\n",
    "data_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\admin\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "data_train.drop(['year'], axis=1, inplace=True)\n",
    "data_train = data_train.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 建立基于模型的推荐系统"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the  0 -th  step is running\n",
      "the rmse of this step on train data is  0.9997633445640969\n",
      "the  1 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507001\n",
      "the  2 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506918\n",
      "the  3 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506855\n",
      "the  4 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507344\n",
      "the  5 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507433\n",
      "the  6 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507394\n",
      "the  7 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507422\n",
      "the  8 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507422\n",
      "the  9 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506795\n",
      "the  10 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507078\n",
      "the  11 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507682\n",
      "the  12 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506945\n",
      "the  13 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507101\n",
      "the  14 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507025\n",
      "the  15 -th  step is running\n",
      "the rmse of this step on train data is  0.999533083450732\n",
      "the  16 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507162\n",
      "the  17 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506846\n",
      "the  18 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507177\n",
      "the  19 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507207\n",
      "the  20 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507187\n",
      "the  21 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507027\n",
      "the  22 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507463\n",
      "the  23 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507173\n",
      "the  24 -th  step is running\n",
      "the rmse of this step on train data is  0.999533083450707\n",
      "the  25 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507338\n",
      "the  26 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507029\n",
      "the  27 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507262\n",
      "the  28 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507387\n",
      "the  29 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507222\n",
      "the  30 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507383\n",
      "the  31 -th  step is running\n",
      "the rmse of this step on train data is  0.999533083450692\n",
      "the  32 -th  step is running\n",
      "the rmse of this step on train data is  0.999533083450664\n",
      "the  33 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506873\n",
      "the  34 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507593\n",
      "the  35 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507412\n",
      "the  36 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507677\n",
      "the  37 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506732\n",
      "the  38 -th  step is running\n",
      "the rmse of this step on train data is  0.999533083450712\n",
      "the  39 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507253\n",
      "the  40 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506626\n",
      "the  41 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506776\n",
      "the  42 -th  step is running\n",
      "the rmse of this step on train data is  0.999533083450708\n",
      "the  43 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507529\n",
      "the  44 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506881\n",
      "the  45 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507307\n",
      "the  46 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507352\n",
      "the  47 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507181\n",
      "the  48 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834507042\n",
      "the  49 -th  step is running\n",
      "the rmse of this step on train data is  0.9995330834506873\n"
     ]
    }
   ],
   "source": [
    "song_SVD_CF = SVD_CF(data_train)\n",
    "song_SVD_CF.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
